<?php
session_start();
require_once '../config.php';

require_once '../function.php';
xiu_get_current_user();


$current_id = $_SESSION['current_login_user']['id'];
$current_pwd = $_SESSION['current_login_user']['password'];

if ($_SERVER['REQUEST_METHOD']==='POST'){
    password_res();
}

function password_res(){
    global $message;
    global $current_pwd;
    global $current_id;

    if (empty($_POST['old'])){
        $message = '密码为空';
        return;
    }

    if (md5($_POST['old']) !== $current_pwd){
        $message = '与当前用户密码不一致';
        return;
    }

    if (empty($_POST['new'])){
        $message = '请输入新的密码';
        return;
    }

    if (empty($_POST['confirm']) || $_POST['confirm'] !== $_POST['new']){
        $message = '请确认密码或者与新的密码不一致';
        return;
    }

    $new_pwd = md5($_POST['new']);


    xiu_IDU("update users  set `password` = '{$new_pwd}' where id = {$current_id};");

    header('Location:/admin/login.php');
}



?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>Password reset &laquo; Admin</title>
  <link rel="stylesheet" href="/static/assets/vendors/bootstrap/css/bootstrap.css">
  <link rel="stylesheet" href="/static/assets/vendors/font-awesome/css/font-awesome.css">
  <link rel="stylesheet" href="/static/assets/vendors/nprogress/nprogress.css">
  <link rel="stylesheet" href="/static/assets/css/admin.css">
  <script src="/static/assets/vendors/nprogress/nprogress.js"></script>
</head>
<body>
  <script>NProgress.start()</script>

  <div class="main">
    <?php include 'inc/navbar.php'; ?>

    <div class="container-fluid">
      <div class="page-title">
        <h1>修改密码</h1>
      </div>
      <!-- 有错误信息时展示 -->
        <?php if (isset($message)):?>
      <div class="alert alert-danger">
        <strong><?php echo $message;?></strong>
      </div>
        <?php endif;?>
      <form class="form-horizontal" action="/admin/password-reset.php" method="post">
        <div class="form-group">
          <label for="old" class="col-sm-3 control-label">旧密码</label>
          <div class="col-sm-7">
            <input id="old" class="form-control" type="password" placeholder="旧密码" name="old">
          </div>
        </div>
        <div class="form-group">
          <label for="password" class="col-sm-3 control-label">新密码</label>
          <div class="col-sm-7">
            <input id="password" class="form-control" type="password" placeholder="新密码" name="new">
          </div>
        </div>
        <div class="form-group">
          <label for="confirm" class="col-sm-3 control-label">确认新密码</label>
          <div class="col-sm-7">
            <input id="confirm" class="form-control" type="password" placeholder="确认新密码" name="confirm">
          </div>
        </div>
        <div class="form-group">
          <div class="col-sm-offset-3 col-sm-7">
            <button type="submit" class="btn btn-primary">修改密码</button>
          </div>
        </div>
      </form>
    </div>
  </div>

  <?php $current_page = 'password-reset'; ?>
  <?php include 'inc/sidebar.php'; ?>

  <script src="/static/assets/vendors/jquery/jquery.js"></script>
  <script src="/static/assets/vendors/bootstrap/js/bootstrap.js"></script>

  <script>




  </script>
  <script>NProgress.done()</script>
</body>
</html>
